package com.songtech.basedao.controller;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.songtech.base.common.BaseConstant;
import com.songtech.base.common.JsonResult;
import com.songtech.basedao.model.BaseDaoTest1;
import com.songtech.basedao.service.BaseServiceImpl1;

/**
 * 测试dao基本功能
 * <br>使用方式一、
 * <br>直接使用基本数据访问的dao
 * <br>service中注入格式：@Resource(name = "com.songtech.basedao.model.BaseDaoTest1")，
 * <br>com.songtech.basedao.model.BaseDaoTest1是要注入的dao将会处理的entity对象
 */
@Controller
@RequestMapping("/basedao1")
public class BaseDaoController1 {
	@Autowired
	private BaseServiceImpl1 serviceDaoImpl;
	
	/**
	 * 添加一条数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/addOne/{id}")
	@ResponseBody
	public JsonResult saveOne(@PathVariable("id") Integer id){
		BaseDaoTest1 test = this.getOneBaseCoreDaoTest(id);
		int count = 0;
		try {
			count = serviceDaoImpl.insert(test);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 添加多条数据
	 * @param num
	 * @param id
	 * @return
	 */
	@RequestMapping("/addSize/{num}/{id}")
	@ResponseBody
	public JsonResult saveSize(@PathVariable("num") Integer num, @PathVariable("id") Integer id){
		int count = 0;
		if(num != null && num > 0){
			List<BaseDaoTest1> list = new ArrayList<>();
			for(int i = 0; i < num; i ++){
				list.add(this.getOneBaseCoreDaoTest(id ++));
			}
			try {
				count = serviceDaoImpl.insert(list);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 添加一条数据(插入的主键使用UUID)
	 * @param id
	 * @return
	 */
	@RequestMapping("/addOneSetUUID/{id}")
	@ResponseBody
	public JsonResult saveOneSetUUID(@PathVariable("id") Integer id){
		BaseDaoTest1 test = this.getOneBaseCoreDaoTest(id);
		int count = 0;
		try {
			count = serviceDaoImpl.insertSetUUID(test);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 添加多条数据(插入的主键使用UUID)
	 * @param num
	 * @param id
	 * @return
	 */
	@RequestMapping("/addSizeSetUUID/{num}/{id}")
	@ResponseBody
	public JsonResult saveSizeSetUUID(@PathVariable("num") Integer num, @PathVariable("id") Integer id){
		int count = 0;
		if(num != null && num > 0){
			List<BaseDaoTest1> list = new ArrayList<>();
			for(int i = 0; i < num; i ++){
				list.add(this.getOneBaseCoreDaoTest(id ++));
			}
			try {
				count = serviceDaoImpl.insertSetUUID(list);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/delOne/{id}")
	@ResponseBody
	public JsonResult delOne(@PathVariable("id") String id){
		int count = 0;
		try {
			count = serviceDaoImpl.delete(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 传入id创建对象，根据对象删除数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/delWhere/{id}")
	@ResponseBody
	public JsonResult delWhere(@PathVariable("id") String id){
		int count = 0;
		if(id != null){
			BaseDaoTest1 test = new BaseDaoTest1();
			test.setId(id);
			try {
				count = serviceDaoImpl.delete(test);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 传入id和name创建对象，根据对象删除数据
	 * @param id
	 * @param name
	 * @return
	 */
	@RequestMapping("/delWhere/{id}/{name}")
	@ResponseBody
	public JsonResult delWhere(@PathVariable("id") String id, @PathVariable("name") String name){
		int count = 0;
		if(id != null){
			BaseDaoTest1 test = new BaseDaoTest1();
			test.setId(id);
			test.setName(name);
			try {
				count = serviceDaoImpl.delete(test);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 更新不为null的字段
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateNotNullParam/{id}")
	@ResponseBody
	public JsonResult updateNotNullParam(@PathVariable("id") String id){
		int count = 0;
		if(id != null){
			BaseDaoTest1 test = this.getOneBaseCoreDaoTest(id);
			try {
				count = serviceDaoImpl.updateLocal(test);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 更新所有字段(包括为空的字段)
	 * @param id
	 * @return
	 */
	@RequestMapping("/updateAllParam/{id}")
	@ResponseBody
	public JsonResult updateAllParam(@PathVariable("id") String id){
		int count = 0;
		if(id != null){
			BaseDaoTest1 test = this.getOneBaseCoreDaoTest(id);
			try {
				count = serviceDaoImpl.update(test);
			} catch (SQLException e) {
				e.printStackTrace();
				return new JsonResult(count, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
			}
		}
		return new JsonResult(count, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 根据id查询数据
	 * @param id
	 * @return
	 */
	@RequestMapping("/selectOne/{id}")
	@ResponseBody
	public JsonResult selectOne(@PathVariable("id") String id){
		BaseDaoTest1 bdt1 = null;
		try {
			bdt1 = serviceDaoImpl.selectById(id);
		} catch (SQLException e) {
			e.printStackTrace();
			return new JsonResult(bdt1, BaseConstant.FAIL_CODE, BaseConstant.FAIL);
		}
		return new JsonResult(bdt1, BaseConstant.SUCCESS_CODE, BaseConstant.SUCCESS);
	}
	
	/**
	 * 生成测试添加的实体信息
	 * @param size
	 * @return
	 */
	private BaseDaoTest1 getOneBaseCoreDaoTest(Integer size){
		BaseDaoTest1 test = new BaseDaoTest1();
		test.setId("T0000000" + size);
		test.setName("测试00" + size);
		test.setInserttime(new Date());
		test.setUpdatetime(new Date());
		test.setRemark("测试00" + size + "备注");
		test.setSize(size);
		test.setTest("test00" + size);
		
		return test;
	}
	
	/**
	 * 生成测试更新的实体信息，随机更新信息
	 * @param size
	 * @return
	 */
	private BaseDaoTest1 getOneBaseCoreDaoTest(String id){
		Random r = new Random();
		Integer num = r.nextInt(4);
		
		BaseDaoTest1 test = new BaseDaoTest1();
		test.setId(id);
		if(num != 1){
			test.setName("测试，更新id=" + id);
		}
		test.setUpdatetime(new Date());
		if(num != 2){
			test.setRemark("测试，更新id=" + id + "，备注");
		}
		test.setSize(r.nextInt());
		if(num != 3){
			test.setTest("test，更新id=" + id);
		}
		
		return test;
	}
}
